<!DOCTYPE html>
<html>
<head>
   <meta name="google-translate-customization" content="457c7458d78f1fda-c04c9be73e1a028e-g6dbca1555c3b1007-18"></meta> 
   <LINK href="style.css" rel="stylesheet" type="text/css">
   <title>Mariachi Casa Menu</title>
   <style>
      body {
         background-image:url('chips-and-salsa.jpg');
         background-repeat:no-repeat;
      }
   </style>
</head>

<!-- ------------------------------------------------------------------- -->
<script>
function help(){
	alert("A waiter is on the way to help you.");
	//send a note to the waiters that this tablet needs help
}

function refill(){
	//refill screen popup
	window.open('../refill_page/refillScreen.html','refillWindow','titlebar=no, menubar=no, toolbar=no');
}
</script>
<!-- ------------------------------------------------------------------- -->

<body>

<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'es', layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

<?php

	//constraints
	require_once 'instructions.php'; //load the menu
	session_start(); //gain access to global data

	//eliminate all of the menu sessions
	if($_POST['logout']) {
		unset($_SESSION['right_side']);
		unset($_SESSION['left_side']);
		unset($_SESSION['left_side_item']);
		unset($_SESSION['left_side_object']);
		unset($_SESSION['bottom_side']);
		unset($_SESSION['bottom_side_item']);
	}

	//handle item modifications
	if($_POST['commit']) {
		//echo "<pre>" . print_r($_POST,true) . "</pre>\n";

		//loop through the form dealing with items to remove first
		foreach($_POST as $index => $value) {

			//match the unwanted items
			if(preg_match('/^without/',$index)) {
				$_SESSION['left_side_object'][$_POST['left']]->possible_items(array($value));
				$_SESSION['left_side_object'][$_POST['left']]->modify(array($value));
				unset($_SESSION['bottom_side']);
				unset($_SESSION['bottom_side_item']);
			}

			//match the desired items
			elseif(preg_match('/^contain/',$index)) {
				$_SESSION['left_side_object'][$_POST['left']]->modify(array(),array(),array($value),$rows);
				$_SESSION['left_side_object'][$_POST['left']]->remove_possible(array($value));
				unset($_SESSION['bottom_side']);
				unset($_SESSION['bottom_side_item']);
			}
		} //conclude loop
	}

	//return to main page
	echo "<form method=\"post\" action=\"../home_page/home.php\" >\n";
        echo "<button class=\"backButton\" onclick\"\">Back</button>\n";
	//echo "<input type=\"submit\" name=\"return\" value=\"Back\" />\n";
	echo "</form>\n";

        // Search bar
        echo "<div id=\"searchBar\">\n";
        echo "<form method=\"post\">\n";
        echo "<input type=\"text\" name=\"search\" id=\"textField\" size=\"80\" value=\"Search for a specific ingredient by typing its name here.\"/>\n";
        echo "<button class=\"backButton\" onclick=\"Javascript:changeSearchField\">Search</button>\n";
        echo "</form>\n";
        echo "</div>\n";

        //*********************************************
        // Stored the value of the input box as a 
        // variable.  Could not continue working on
        // this due to some menu functionality issues.
        // Still needs finished!
        //*********************************************
        if($_POST['search']) {
           echo "<div>Searching!</div>\n";
           $searchKey = $_POST['search'];
           echo "$searchKey";
        }

        echo "<h1>Menu</h1>";

	//table start
	$spacing = count($master)+1;
	echo "<table>\n<tr>\n";

	//delete an item from the list
	if($_POST['delete']) { 

		//case when user deletes item while modifying
		if($_SESSION['left_side_item'][$_POST['selected']] == 
			$_SESSION['bottom_side_item']) {
			unset($_SESSION['bottom_side_item']);
			unset($_SESSION['bottom_side']);
		}

		//delete a selected item
		unset($_SESSION['left_side'][$_POST['selected']]);
		unset($_SESSION['left_side_item'][$_POST['selected']]);
		unset($_SESSION['left_side_object'][$_POST['selected']]);
		
		//case user deletes all items while still modifying
		if(!$_SESSION['left_side'] && !$_SESSION['left_side_item'])
			unset($_SESSION['bottom_side']);
	}

	//keep track of selected unordered items
	if($_POST['static'] || $_SESSION['left_side']) {
		if($_POST['static']) { //case selected items are added
			$_SESSION['left_side'][] = "<option value=\"";
			$_SESSION['left_side_item'][] = $_POST['static']; //hold values
			$_SESSION['left_side_object'][] = $master[$_POST['arrN']][$_POST['static']];
			for($i = 0; $i < count($_SESSION['left_side']); ++$i)
				$_SESSION['left_side'][$i] .= "$i\" >" . $_POST['static'] . 
				"</option>\n";
		} // end case

		//case a session is active. show selected elements
		//echo "<td rowspan=\"6\"><b>Current Order</b>:<br/>\n";
		echo "<td rowspan=\"$spacing\"><h2><b>Current Order:</b></h2>\n";
		echo "<form method=\"post\">\n";
		echo "<select size=\"15\" name=\"selected\" style=\"width:100%;font-size:large\">\n";	
		foreach($_SESSION['left_side'] as $value) echo $value;
		echo "</select><br/>\n";

		//submit deletion or modifications
		echo "<input type=\"submit\" name=\"modify\" value=\"Modify\" class=\"footerButton\">\n";
		echo "<input type=\"submit\" name=\"delete\" value=\"Delete\" class=\"footerButton\"/>\n";
		echo "</form>\n";
		echo "</td>\n";
	}

	//selecting meal
	echo "</tr>\n";

	//appetizers
	echo "<tr>\n";
	echo "<form method=\"post\">\n";
	echo "<td><input type=\"submit\" name=\"appetizers\" value=\"Appetizers\" class=\"menuButton\"></td>\n";
	echo "</form>\n";

/***********************************contain*****************************************/
	//rerender the page
	foreach($master as $index => $category) {
		//echo "<pre>here:" . print_r($master[$index],true) . "</pre>\n";
		//if($_POST[$index] || $_SESSION['right_side']) {
		if(isset($_POST[$index])) {
			//$str = "<td rowspan=\"" . count($master) . "\">\n"; //for visual
			$str = "<td rowspan=\"$spacing\">\n"; //for visual

			//update right side of table with options
			foreach($category as $jIndex => $value) 
				$str .= "<form method=\"post\">\n" .
					"<input type=\"submit\" name=\"$jIndex\" value=\"\" />" .
					"<input type=\"hidden\" name=\"static\" value=\"$jIndex\" />" .
					"<input type=\"hidden\" name=\"arrN\" value=\"$index\" />" .
					"<b>$value->cost</b> " . $value->description . "<br/>\n" .
					"</form>\n";
			$str .= "</td>\n"; //conclude the re-render
			//echo $str; //output html

			//make part of session
			$_SESSION['right_side'] = $str;
			break; //exit the loop
		} //conclude page re-render
	}
	echo $_SESSION['right_side'];
	//if($_SESSION['right_side']) echo $_SESSION['right_side']; //already done
	/*
	//rerender the page. exists only after processed forms
	if($_POST['appetizers'] || $_SESSION['right_side']) {	
		$str = "<td rowspan=\"4\">\n";

		//update right side of table with options
		foreach($appetizers as $index => $value) 
			$str .= "<form method=\"post\">\n" .
				"<input type=\"submit\" name=\"$index\" value=\"\" />" .
				"<input type=\"hidden\" name=\"static\" value=\"$index\" />" .
				"<b>$value->cost</b> " . $value->description . "<br/>\n" .
				"</form>\n";
		$str .= "</td>\n"; //conclude the re-render
		echo $str; //output html

		//make part of session
		$_SESSION['right_side'] = $str;
	} */
	echo "</tr>\n";
/*********************************endcontain***************************************/

	//steaks form
	echo "<form method=\"post\">\n";
	echo "<tr>\n<td><input type=\"submit\" name=\"steaks\" value=\"Steaks\" class=\"menuButton\"/></td>\n</tr>\n";
	echo "</form>\n"; // conclude steaks form

	//tacos form
	echo "<form method=\"post\">\n";
	echo "<tr>\n<td><input type=\"submit\" name=\"tacos\" value=\"Tacos\" class=\"menuButton\"/></td>\n</tr>\n";
	echo "</form>\n";

	//entrees form
	echo "<form method=\"post\">\n";
	echo "<tr>\n<td><input type=\"submit\" name=\"entrees\" value=\"Entrees\" class=\"menuButton\"/></td>\n</tr>\n";
	echo "</form>\n";

	//healthy form
	echo "<form method=\"post\">\n";
	echo "<tr>\n<td><input type=\"submit\" name=\"healthy\" value=\"Healthy\" class=\"menuButton\"/></td>\n</tr>\n";
	echo "</form>\n";

	//Desserts
	echo "<form method=\"post\">\n";
	echo "<tr>\n<td><input type=\"submit\" name=\"desserts\" value=\"Desserts\" class=\"menuButton\"/></td>\n</tr>\n";
	echo "</form>\n";
	
	//case of modification
	if($_POST['modify'] && isset($_POST['selected'])) {

		//ingredients user doesn't want that are on the plate
		$str = '';
		$str .= "<form method=\"post\"/>\n";
		$str .= "<tr></tr>"; //dummy values for rendering
		$str .= "<tr>\n<td colspan=\"3\"><b>Without:&nbsp;</b><br/>\n";

		//output options for user to remove with checkboxes
		$str .= $_SESSION['left_side_object'][$_POST['selected']]->user_output_with();
		$str .= "<input type=\"hidden\" name=\"left\" value=\"{$_POST['selected']}\" />\n";

		//output options for user to add with checkboxes
		$str .= "</td>\n</tr>\n";
		$str .= "<tr>\n<td colspan=\"3\"><b>Add:&nbsp;</b><br/>\n";
		$str .= $_SESSION['left_side_object'][$_POST['selected']]->possible_output();
		$str .= "</td>\n</tr>\n";


		//submit changes
		$str .= "<tr>\n<td colspan=\"3\"><input type=\"submit\" name=\"commit\"" 			. " value=\"Apply Changes\" class=\"backButton\"/></td>\n</td>\n";
		$str .= "</form>\n";

		$_SESSION['bottom_side'] = $str;
		$_SESSION['bottom_side_item'] = $_SESSION['left_side_item'][$_POST['selected']];
		echo $str;

	} //end mofify case

	//memorize selected item to modify
	else if($_SESSION['bottom_side'] && $_SESSION['left_side']) 
		echo $_SESSION['bottom_side'];

	echo "</table>\n";

	//order
	if($_SESSION['left_side']) {

		//form to process
		echo "<form method=\"post\" action=\"../home_page/home.php\">\n";
		echo "<input type=\"submit\" name=\"order\" value=\"Order\" class=\"backButton\"/>\n";
		echo "</form>\n";
	} //conclude order

/*	echo "<form method=\"post\">\n";
	echo "<input type=\"submit\" name=\"logout\" value=\"logout\" class=\"backButton\"/>\n";
	echo "</form>\n";*/

   echo "<br/><br/><br/><br/>";
   echo "<div id=\"pageBottom\">";
   echo "<button class=\"footerButton\" onclick=\"\help()">Help</button>";
   echo "<button class=\"footerButton\" onclick=\"\refill()">Refill</button>";
   echo "</div>";
?>
</body>

</html>
